package com.wish.controller;

import com.aliyuncs.exceptions.ClientException;
import com.wish.util.FileOSSUtil;
import com.wish.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@RequestMapping("/wish/upload")
@Slf4j
@Component
public class UploadController {

    // 阿里云API的内或外网域名

    @Value("${aliyun.oss.endpoint}")
    String endpoint;

    // 阿里云API的密钥Access Key ID

    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;

    // 阿里云API的密钥Access Key Secret

    @Value("${aliyun.oss.accessKeySecret}")
    String accessKeySecret;

    // 阿里云API的bucket名称

    @Value("${aliyun.oss.bucketName}")
    String bucketName;

    @RequestMapping("pic")
    public ResultEntity uploadPic(MultipartFile file) throws IOException, ClientException {
        log.info("文件："+file.getOriginalFilename());
        try{
            FileOSSUtil.settingIniOSS(endpoint,accessKeyId,accessKeySecret,bucketName);
            FileOSSUtil.uploadFile(file.getOriginalFilename(),file);
            return ResultEntity.ok("200","上传成功","http://"+bucketName+"."+endpoint+"/"+file.getOriginalFilename());
        }catch (Exception e){
            return ResultEntity.ok("500","上传中遇到错误");
        }
    }
}
